﻿using Sidvall.Serialization;
using System.Linq;

namespace Sidvall.WorkItemManager.Data.Model.EntityManagers
{
    public sealed partial class WorkItemManager<TEntity, TList> : global::Sidvall.Data.EntityManagerBase
        where TEntity : global::Sidvall.WorkItemManager.Data.IWorkItem
        where TList : Sidvall.Data.IDataRowProvider<IWorkItem>
    {
        #region Public Members

        #region AcceptChanges

        public void AcceptChanges(global::Sidvall.WorkItemManager.Data.IWorkItem value)
        {
            AcceptChanges(value, null);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
        public void AcceptChanges(global::Sidvall.WorkItemManager.Data.IWorkItem value, Sidvall.Data.AcceptChangesSettings acceptChangesSettings)
        {
            if (value == null)
                return;
            if (value.DataRowStateIsDirty)
            {
                if (acceptChangesSettings == null)
                    acceptChangesSettings = new Sidvall.Data.AcceptChangesSettings();
                Sidvall.Data.EntityManager.AcceptItemChanges(value, acceptChangesSettings);
                this.EntityFacadeManager.GenericEntityFacade.WorkItemLogManager.AcceptChanges(value.WorkItemLogs, acceptChangesSettings);
                this.EntityFacadeManager.GenericEntityFacade.WorkItemSensorManager.AcceptChanges(value.WorkItemSensors, acceptChangesSettings);
                this.EntityFacadeManager.GenericEntityFacade.WorkItemWorkConditionManager.AcceptChanges(value.WorkItemWorkConditions, acceptChangesSettings);
                this.EntityFacadeManager.GenericEntityFacade.WorkerManager.AcceptChanges(value.Worker, acceptChangesSettings);
                this.EntityFacadeManager.GenericEntityFacade.WorkItemTypeManager.AcceptChanges(value.WorkItemType, acceptChangesSettings);
                this.EntityFacadeManager.GenericEntityFacade.WorkRetryPolicyManager.AcceptChanges(value.WorkRetryPolicy, acceptChangesSettings);
            }
        }
        public void AcceptChanges(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> value)
        {
            AcceptItemsChanges(value, null);
        }
        public void AcceptChanges(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> value, Sidvall.Data.AcceptChangesSettings acceptChangesSettings)
        {
            AcceptItemsChanges(value, acceptChangesSettings);
        }

        #endregion
        #region SetBusinessSettings

        public void SetBusinessSettings(global::Sidvall.WorkItemManager.Data.IWorkItem value, global::Sidvall.Business.BusinessSettings businessSettings, global::Sidvall.Business.BusinessCollectionSettings businessCollectionSettings)
        {
            if (value == null)
                return;
            var provider = value as global::Sidvall.Business.IBusinessSettingsProvider;
            if (provider != null)
                provider.BusinessSettings.Import(businessSettings);
            this.EntityFacadeManager.GenericEntityFacade.WorkItemLogManager.SetBusinessSettings(value.WorkItemLogs, businessSettings, businessCollectionSettings);
            this.EntityFacadeManager.GenericEntityFacade.WorkItemSensorManager.SetBusinessSettings(value.WorkItemSensors, businessSettings, businessCollectionSettings);
            this.EntityFacadeManager.GenericEntityFacade.WorkItemWorkConditionManager.SetBusinessSettings(value.WorkItemWorkConditions, businessSettings, businessCollectionSettings);
            this.EntityFacadeManager.GenericEntityFacade.WorkerManager.SetBusinessSettings(value.Worker, businessSettings, businessCollectionSettings);
            this.EntityFacadeManager.GenericEntityFacade.WorkItemTypeManager.SetBusinessSettings(value.WorkItemType, businessSettings, businessCollectionSettings);
            this.EntityFacadeManager.GenericEntityFacade.WorkRetryPolicyManager.SetBusinessSettings(value.WorkRetryPolicy, businessSettings, businessCollectionSettings);
        }
        public void SetBusinessSettings(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> value, global::Sidvall.Business.BusinessSettings businessSettings, global::Sidvall.Business.BusinessCollectionSettings businessCollectionSettings)
        {
            if (value == null)
                return;
            var provider = value as global::Sidvall.Business.IBusinessCollectionSettingsProvider;
            if (provider != null)
                provider.BusinessSettings.Import(businessCollectionSettings);
            foreach (var item in value)
                SetBusinessSettings(item, businessSettings, businessCollectionSettings);
        }

        #endregion
        #region GetDataRowStateCounts

        public global::Sidvall.Data.DataRowStateCountItemCollection GetDataRowStateCounts(global::Sidvall.WorkItemManager.Data.IWorkItem value)
        {
            var items = new global::Sidvall.Data.DataRowStateCountItemCollection();
            LoadDataRowStateCounts(items, value);
            return items;
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        internal void LoadDataRowStateCounts(global::Sidvall.Data.DataRowStateCountItemCollection items, global::Sidvall.WorkItemManager.Data.IWorkItem value)
        {
            if (value == null)
                return;
            items.Add(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem, value);
            this.EntityFacadeManager.GenericEntityFacade.WorkItemLogManager.LoadDataRowStateCounts(items, value.WorkItemLogs);
            this.EntityFacadeManager.GenericEntityFacade.WorkItemSensorManager.LoadDataRowStateCounts(items, value.WorkItemSensors);
            this.EntityFacadeManager.GenericEntityFacade.WorkItemWorkConditionManager.LoadDataRowStateCounts(items, value.WorkItemWorkConditions);
            this.EntityFacadeManager.GenericEntityFacade.WorkerManager.LoadDataRowStateCounts(items, value.Worker);
            this.EntityFacadeManager.GenericEntityFacade.WorkItemTypeManager.LoadDataRowStateCounts(items, value.WorkItemType);
            this.EntityFacadeManager.GenericEntityFacade.WorkRetryPolicyManager.LoadDataRowStateCounts(items, value.WorkRetryPolicy);
        }
        public global::Sidvall.Data.DataRowStateCountItemCollection GetDataRowStateCounts(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> value)
        {
            var items = new global::Sidvall.Data.DataRowStateCountItemCollection();
            LoadDataRowStateCounts(items, value);
            return items;
        }
        internal void LoadDataRowStateCounts(global::Sidvall.Data.DataRowStateCountItemCollection items, System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> value)
        {
            if (value == null)
                return;
            items.Add(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem, value as Sidvall.Data.IListContextProvider);
            foreach (var item in value)
                LoadDataRowStateCounts(items, item);
        }

        #endregion
        #region LoadInstanceField

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public void LoadInstanceField(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItem> items, string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            if (items == null)
                return;
            foreach (var item in items)
                LoadInstanceField(item, fieldName, value, cascade, raisePropertyChanged, updateState);
        }
        partial void OnAfterLoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState);
        partial void OnBeforeLoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState);
        partial void OnLoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "cascade"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1505:AvoidUnmaintainableCode")]
        public void LoadInstanceField(global::Sidvall.WorkItemManager.Data.IWorkItem item, string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            if (item == null)
                return;
            if (fieldName == null)
                return;
            OnBeforeLoadInstanceField(fieldName, value, cascade, raisePropertyChanged, updateState);
            switch (fieldName)
            {
                case "CanceledAction":
                    if (value == null)
                        return;
                    item.CanceledAction = (global::Sidvall.WorkItemManager.Workflow.ThreadAction)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "CanceledAction", raisePropertyChanged, updateState);
                    break;
                case "Description":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, null, true);
                    item.Description = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Description", raisePropertyChanged, updateState);
                    break;
                case "ExecutionCompletionPercentage":
                    if (value == null)
                        return;
                    value = Sidvall.Business.ValidationManager.GetValidByte(value, null, null);
                    item.ExecutionCompletionPercentage = (byte)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "ExecutionCompletionPercentage", raisePropertyChanged, updateState);
                    break;
                case "ExecutionContext":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, null, true);
                    item.ExecutionContext = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "ExecutionContext", raisePropertyChanged, updateState);
                    break;
                case "ExecutionContextUpdated":
                    value = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value, System.DateTimeKind.Utc);
                    item.ExecutionContextUpdated = (System.Nullable<System.DateTime>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "ExecutionContextUpdated", raisePropertyChanged, updateState);
                    break;
                case "ExecutionOrder":
                    if (value == null)
                        return;
                    value = Sidvall.Business.ValidationManager.GetValidInt32(value, null, null);
                    item.ExecutionOrder = (int)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "ExecutionOrder", raisePropertyChanged, updateState);
                    break;
                case "ExecutionStartTime":
                    value = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value, System.DateTimeKind.Utc);
                    item.ExecutionStartTime = (System.Nullable<System.DateTime>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "ExecutionStartTime", raisePropertyChanged, updateState);
                    break;
                case "FailedAction":
                    if (value == null)
                        return;
                    item.FailedAction = (global::Sidvall.WorkItemManager.Workflow.ThreadAction)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "FailedAction", raisePropertyChanged, updateState);
                    break;
                case "FailedExecutionCount":
                    if (value == null)
                        return;
                    value = Sidvall.Business.ValidationManager.GetValidInt32(value, null, null);
                    item.FailedExecutionCount = (int)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "FailedExecutionCount", raisePropertyChanged, updateState);
                    break;
                case "ForceExecution":
                    if (value == null)
                        return;
                    item.ForceExecution = (bool)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "ForceExecution", raisePropertyChanged, updateState);
                    break;
                case "IsCanceled":
                    if (value == null)
                        return;
                    item.IsCanceled = (bool)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "IsCanceled", raisePropertyChanged, updateState);
                    break;
                case "IsEnabled":
                    if (value == null)
                        return;
                    item.IsEnabled = (bool)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "IsEnabled", raisePropertyChanged, updateState);
                    break;
                case "LastExecutionEndTime":
                    value = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value, System.DateTimeKind.Utc);
                    item.LastExecutionEndTime = (System.Nullable<System.DateTime>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "LastExecutionEndTime", raisePropertyChanged, updateState);
                    break;
                case "LastExecutionStartTime":
                    value = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value, System.DateTimeKind.Utc);
                    item.LastExecutionStartTime = (System.Nullable<System.DateTime>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "LastExecutionStartTime", raisePropertyChanged, updateState);
                    break;
                case "LastResultMessage":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, null, true);
                    item.LastResultMessage = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "LastResultMessage", raisePropertyChanged, updateState);
                    break;
                case "LastWorkItemResultId":
                    item.LastWorkItemResultId = (System.Nullable<global::Sidvall.WorkItemManager.Workflow.WorkItemResultType>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "LastWorkItemResultId", raisePropertyChanged, updateState);
                    break;
                case "LogDayCount":
                    if (value == null)
                        return;
                    value = Sidvall.Business.ValidationManager.GetValidInt32(value, null, null);
                    item.LogDayCount = (int)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "LogDayCount", raisePropertyChanged, updateState);
                    break;
                case "LogModeId":
                    if (value == null)
                        return;
                    item.LogModeId = (global::Sidvall.WorkItemManager.Workflow.LogModes)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "LogModeId", raisePropertyChanged, updateState);
                    break;
                case "Name":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, 255, true);
                    item.Name = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Name", raisePropertyChanged, updateState);
                    break;
                case "RetryCount":
                    if (value == null)
                        return;
                    value = Sidvall.Business.ValidationManager.GetValidInt32(value, null, null);
                    item.RetryCount = (int)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "RetryCount", raisePropertyChanged, updateState);
                    break;
                case "RetryTime":
                    value = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value, System.DateTimeKind.Utc);
                    item.RetryTime = (System.Nullable<System.DateTime>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "RetryTime", raisePropertyChanged, updateState);
                    break;
                case "SensorLogDayCount":
                    if (value == null)
                        return;
                    value = Sidvall.Business.ValidationManager.GetValidInt32(value, null, null);
                    item.SensorLogDayCount = (int)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "SensorLogDayCount", raisePropertyChanged, updateState);
                    break;
                case "WorkerId":
                    if (value == null)
                        return;
                    value = Sidvall.Business.ValidationManager.GetValidInt64(value, null, null);
                    item.WorkerId = (long)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkerId", raisePropertyChanged, updateState);
                    if (cascade)
                    {
                        if (item.Worker != null)
                        {
                            if (item.Worker.WorkerId != item.WorkerId)
                                item.Worker = null;
                        }
                    }
                    break;
                case "WorkItemContext":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, null, true);
                    item.WorkItemContext = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemContext", raisePropertyChanged, updateState);
                    break;
                case "WorkItemId":
                    value = Sidvall.Business.ValidationManager.GetValidInt64(value, null, null);
                    item.WorkItemId = (System.Nullable<long>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemId", raisePropertyChanged, updateState);
                    if (cascade)
                    {
                        this.EntityFacadeManager.GenericEntityFacade.WorkItemLogManager.LoadInstanceField(item.WorkItemLogs, "WorkItemId", value, cascade, raisePropertyChanged, updateState);
                        this.EntityFacadeManager.GenericEntityFacade.WorkItemSensorManager.LoadInstanceField(item.WorkItemSensors, "WorkItemId", value, cascade, raisePropertyChanged, updateState);
                        this.EntityFacadeManager.GenericEntityFacade.WorkItemWorkConditionManager.LoadInstanceField(item.WorkItemWorkConditions, "WorkItemId", value, cascade, raisePropertyChanged, updateState);
                    }
                    break;
                case "WorkItemStatusId":
                    if (value == null)
                        return;
                    item.WorkItemStatusId = (global::Sidvall.WorkItemManager.Workflow.WorkItemStatus)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemStatusId", raisePropertyChanged, updateState);
                    break;
                case "WorkItemTypeId":
                    value = Sidvall.Business.ValidationManager.GetValidInt64(value, null, null);
                    item.WorkItemTypeId = (System.Nullable<long>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemTypeId", raisePropertyChanged, updateState);
                    if (cascade)
                    {
                        if (item.WorkItemType != null)
                        {
                            if (item.WorkItemType.WorkItemTypeId != item.WorkItemTypeId)
                                item.WorkItemType = null;
                        }
                    }
                    break;
                case "WorkRetryPolicyContext":
                    value = Sidvall.Business.ValidationManager.GetValidString(value, false, null, true);
                    item.WorkRetryPolicyContext = (string)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkRetryPolicyContext", raisePropertyChanged, updateState);
                    break;
                case "WorkRetryPolicyId":
                    value = Sidvall.Business.ValidationManager.GetValidInt64(value, null, null);
                    item.WorkRetryPolicyId = (System.Nullable<long>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkRetryPolicyId", raisePropertyChanged, updateState);
                    if (cascade)
                    {
                        if (item.WorkRetryPolicy != null)
                        {
                            if (item.WorkRetryPolicy.WorkRetryPolicyId != item.WorkRetryPolicyId)
                                item.WorkRetryPolicy = null;
                        }
                    }
                    break;
                case "ServerConnectionId":
                    value = Sidvall.Business.ValidationManager.GetValidInt32(value, null, null);
                    item.ServerConnectionId = (System.Nullable<System.Int32>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "ServerConnectionId", raisePropertyChanged, updateState);
                    break;
                case "WorkItemLogs":
                    item.SetWorkItemLogs((Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemLog>)value);
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemLogs", raisePropertyChanged, updateState);
                    if (cascade)
                    {
                        if (item.WorkItemLogs != null)
                        {
                            this.EntityFacadeManager.GenericEntityFacade.WorkItemLogManager.LoadInstanceField(item.WorkItemLogs, "WorkItemId", item.WorkItemId, cascade, raisePropertyChanged, updateState);
                        }
                    }
                    break;
                case "WorkItemSensors":
                    item.SetWorkItemSensors((Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemSensor>)value);
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemSensors", raisePropertyChanged, updateState);
                    if (cascade)
                    {
                        if (item.WorkItemSensors != null)
                        {
                            this.EntityFacadeManager.GenericEntityFacade.WorkItemSensorManager.LoadInstanceField(item.WorkItemSensors, "WorkItemId", item.WorkItemId, cascade, raisePropertyChanged, updateState);
                        }
                    }
                    break;
                case "WorkItemWorkConditions":
                    item.SetWorkItemWorkConditions((Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemWorkCondition>)value);
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemWorkConditions", raisePropertyChanged, updateState);
                    if (cascade)
                    {
                        if (item.WorkItemWorkConditions != null)
                        {
                            this.EntityFacadeManager.GenericEntityFacade.WorkItemWorkConditionManager.LoadInstanceField(item.WorkItemWorkConditions, "WorkItemId", item.WorkItemId, cascade, raisePropertyChanged, updateState);
                        }
                    }
                    break;
                case "Worker":
                    item.Worker = (global::Sidvall.WorkItemManager.Data.IWorker)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Worker", raisePropertyChanged, updateState);
                    if (cascade)
                    {
                        if (item.Worker != null)
                        {
                            if ((item.Worker.WorkerId != null) && (item.Worker.WorkerId.Value != item.WorkerId))
                                LoadInstanceField(item, "WorkerId", item.Worker.WorkerId, cascade, raisePropertyChanged, updateState);
                        }
                    }
                    break;
                case "WorkItemType":
                    item.WorkItemType = (global::Sidvall.WorkItemManager.Data.IWorkItemType)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkItemType", raisePropertyChanged, updateState);
                    if (cascade)
                    {
                        if (item.WorkItemType != null)
                        {
                            if ((item.WorkItemType.WorkItemTypeId != null) && (item.WorkItemType.WorkItemTypeId.Value != item.WorkItemTypeId))
                                LoadInstanceField(item, "WorkItemTypeId", item.WorkItemType.WorkItemTypeId, cascade, raisePropertyChanged, updateState);
                        }
                    }
                    break;
                case "WorkRetryPolicy":
                    item.WorkRetryPolicy = (global::Sidvall.WorkItemManager.Data.IWorkRetryPolicy)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "WorkRetryPolicy", raisePropertyChanged, updateState);
                    if (cascade)
                    {
                        if (item.WorkRetryPolicy != null)
                        {
                            if ((item.WorkRetryPolicy.WorkRetryPolicyId != null) && (item.WorkRetryPolicy.WorkRetryPolicyId.Value != item.WorkRetryPolicyId))
                                LoadInstanceField(item, "WorkRetryPolicyId", item.WorkRetryPolicy.WorkRetryPolicyId, cascade, raisePropertyChanged, updateState);
                        }
                    }
                    break;
                default:
                    OnLoadInstanceField(fieldName, value, cascade, raisePropertyChanged, updateState);
                    break;
            }
            OnAfterLoadInstanceField(fieldName, value, cascade, raisePropertyChanged, updateState);
        }

        #endregion
        #region Clone

        public TEntity Clone(IWorkItem value)
        {
            return Copy(value, Sidvall.Data.CopySettings.Clone());
        }
        public TList Clone(System.Collections.Generic.IEnumerable<IWorkItem> value)
        {
            return Copy(value, Sidvall.Data.CopySettings.Clone());
        }

        #endregion
        #region Copy

        public TEntity Copy(IWorkItem value)
        {
            return Copy(value, null, string.Empty, string.Empty);
        }
        public TEntity Copy(IWorkItem value, Sidvall.Data.CopySettings copySettings)
        {
            return Copy(value, copySettings, string.Empty, string.Empty);
        }
        internal TEntity Copy(IWorkItem value, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            if (!Sidvall.Data.DataRowContextManager.IsValid(value, copySettings.RowFilter))
                return default(TEntity);
            var item = (TEntity)this.EntityFacadeManager.ResourceFactory.CreateItem(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem, null);
            if (item == null)
                return default(TEntity);
            Import(item, value, copySettings, parent, child);
            return item;
        }

        public TList Copy(System.Collections.Generic.IEnumerable<IWorkItem> value)
        {
            return Copy(value, null, string.Empty, string.Empty);
        }
        public TList Copy(System.Collections.Generic.IEnumerable<IWorkItem> value, Sidvall.Data.CopySettings copySettings)
        {
            return Copy(value, copySettings, string.Empty, string.Empty);
        }
        internal TList Copy(System.Collections.Generic.IEnumerable<IWorkItem> value, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            var items = (TList)this.EntityFacadeManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem);
            Import(items, value, copySettings, parent, child);
            return items;
        }

        #endregion
        #region GetKey

        public string GetKey(IWorkItem item)
        {
            if (item == null)
                return null;
            if (item.WorkItemId == null)
                return null;
            return item.WorkItemId.ToString();
        }
        public string GetKey(long workItemId)
        {
            return workItemId.ToString(System.Globalization.CultureInfo.InvariantCulture);
        }
        public string GetKey(Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField> item)
        {
            if (item == null)
                return null;
            var part = item.FilterItems.GetFilterItemValue(Sidvall.WorkItemManager.Data.Fields.WorkItemField.WorkItemId, null);
            if (part == null)
                return null;
            var value = part;
            return value;
        }

        #endregion
        #region ParseKey

        public System.Nullable<long> ParseKey(string key)
        {
            return key.ToNullableInt64();
        }

        #endregion
        #region Import

        public void Import(TEntity destination, IWorkItem source)
        {
            Import(destination, source, null, string.Empty, string.Empty);
        }
        public void Import(TEntity destination, IWorkItem source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source, copySettings, string.Empty, string.Empty);
        }
        partial void OnAfterImport(TEntity destination, IWorkItem source, Sidvall.Data.CopySettings copySettings, string parent, string child);
        partial void OnBeforeImport(TEntity destination, IWorkItem source, Sidvall.Data.CopySettings copySettings, ref string parent, ref string child);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "parent"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "child"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        internal void Import(TEntity destination, IWorkItem source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            if (destination == null)
                return;
            if (source == null)
                return;
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            OnBeforeImport(destination, source, copySettings, ref parent, ref child);
            var accessLevelProvider = source as Sidvall.Security.IPropertyAccessLevelProvider;
            if ((copySettings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys) == Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys)
            {
                if (copySettings.IncludedProperty(accessLevelProvider, true, true, false, Sidvall.Data.DataType.Int64, "WorkItem.WorkItemId", "WorkItem"))
                    destination.WorkItemId = source.WorkItemId;
            }
            if ((copySettings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys) == Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys)
            {
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.CanceledAction", "WorkItem"))
                    destination.CanceledAction = source.CanceledAction;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.Description", "WorkItem"))
                    destination.Description = source.Description;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Byte, "WorkItem.ExecutionCompletionPercentage", "WorkItem"))
                    destination.ExecutionCompletionPercentage = source.ExecutionCompletionPercentage;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.ExecutionContext", "WorkItem"))
                    destination.ExecutionContext = source.ExecutionContext;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.ExecutionContextUpdated", "WorkItem"))
                    destination.ExecutionContextUpdated = source.ExecutionContextUpdated;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.ExecutionOrder", "WorkItem"))
                    destination.ExecutionOrder = source.ExecutionOrder;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.ExecutionStartTime", "WorkItem"))
                    destination.ExecutionStartTime = source.ExecutionStartTime;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.FailedAction", "WorkItem"))
                    destination.FailedAction = source.FailedAction;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.FailedExecutionCount", "WorkItem"))
                    destination.FailedExecutionCount = source.FailedExecutionCount;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "WorkItem.ForceExecution", "WorkItem"))
                    destination.ForceExecution = source.ForceExecution;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "WorkItem.IsCanceled", "WorkItem"))
                    destination.IsCanceled = source.IsCanceled;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "WorkItem.IsEnabled", "WorkItem"))
                    destination.IsEnabled = source.IsEnabled;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.LastExecutionEndTime", "WorkItem"))
                    destination.LastExecutionEndTime = source.LastExecutionEndTime;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.LastExecutionStartTime", "WorkItem"))
                    destination.LastExecutionStartTime = source.LastExecutionStartTime;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.LastResultMessage", "WorkItem"))
                    destination.LastResultMessage = source.LastResultMessage;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.LastWorkItemResultId", "WorkItem"))
                    destination.LastWorkItemResultId = source.LastWorkItemResultId;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.LogDayCount", "WorkItem"))
                    destination.LogDayCount = source.LogDayCount;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.LogModeId", "WorkItem"))
                    destination.LogModeId = source.LogModeId;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.Name", "WorkItem"))
                    destination.Name = source.Name;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.RetryCount", "WorkItem"))
                    destination.RetryCount = source.RetryCount;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.RetryTime", "WorkItem"))
                    destination.RetryTime = source.RetryTime;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.SensorLogDayCount", "WorkItem"))
                    destination.SensorLogDayCount = source.SensorLogDayCount;
                if (copySettings.IncludedProperty(accessLevelProvider, false, true, false, Sidvall.Data.DataType.Int64, "WorkItem.WorkerId", "WorkItem"))
                    destination.WorkerId = source.WorkerId;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.WorkItemContext", "WorkItem"))
                    destination.WorkItemContext = source.WorkItemContext;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.WorkItemStatusId", "WorkItem"))
                    destination.WorkItemStatusId = source.WorkItemStatusId;
                if (copySettings.IncludedProperty(accessLevelProvider, false, true, false, Sidvall.Data.DataType.Int64, "WorkItem.WorkItemTypeId", "WorkItem"))
                    destination.WorkItemTypeId = source.WorkItemTypeId;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.WorkRetryPolicyContext", "WorkItem"))
                    destination.WorkRetryPolicyContext = source.WorkRetryPolicyContext;
                if (copySettings.IncludedProperty(accessLevelProvider, false, true, false, Sidvall.Data.DataType.Int64, "WorkItem.WorkRetryPolicyId", "WorkItem"))
                    destination.WorkRetryPolicyId = source.WorkRetryPolicyId;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.ServerConnectionId", "WorkItem"))
                    destination.ServerConnectionId = source.ServerConnectionId;
            }
            if (copySettings.IncludeChildren)
            {
                if (child != "WorkItemLog")
                {
                    if (copySettings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkItemLogs", "WorkItemLog"))
                    {
                        if (destination.WorkItemLogs != null)
                            this.EntityFacadeManager.GenericEntityFacade.WorkItemLogManager.Import(destination.WorkItemLogs, source.WorkItemLogs, copySettings, "WorkItem", string.Empty);
                        else if (source.WorkItemLogs != null)
                            destination.SetWorkItemLogs(this.EntityFacadeManager.GenericEntityFacade.WorkItemLogManager.Copy(source.WorkItemLogs, copySettings, "WorkItem", string.Empty));
                    }
                }
                if (child != "WorkItemSensor")
                {
                    if (copySettings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkItemSensors", "WorkItemSensor"))
                    {
                        if (destination.WorkItemSensors != null)
                            this.EntityFacadeManager.GenericEntityFacade.WorkItemSensorManager.Import(destination.WorkItemSensors, source.WorkItemSensors, copySettings, "WorkItem", string.Empty);
                        else if (source.WorkItemSensors != null)
                            destination.SetWorkItemSensors(this.EntityFacadeManager.GenericEntityFacade.WorkItemSensorManager.Copy(source.WorkItemSensors, copySettings, "WorkItem", string.Empty));
                    }
                }
                if (child != "WorkItemWorkCondition")
                {
                    if (copySettings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkItemWorkConditions", "WorkItemWorkCondition"))
                    {
                        if (destination.WorkItemWorkConditions != null)
                            this.EntityFacadeManager.GenericEntityFacade.WorkItemWorkConditionManager.Import(destination.WorkItemWorkConditions, source.WorkItemWorkConditions, copySettings, "WorkItem", string.Empty);
                        else if (source.WorkItemWorkConditions != null)
                            destination.SetWorkItemWorkConditions(this.EntityFacadeManager.GenericEntityFacade.WorkItemWorkConditionManager.Copy(source.WorkItemWorkConditions, copySettings, "WorkItem", string.Empty));
                    }
                }
            }
            if (copySettings.IncludeParents)
            {
                if (parent != "Worker")
                {
                    if (copySettings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.Worker", "Worker"))
                    {
                        if (destination.Worker != null)
                            this.EntityFacadeManager.GenericEntityFacade.WorkerManager.Import(destination.Worker, source.Worker, copySettings, string.Empty, "WorkItem");
                        else if (source.Worker != null)
                            destination.Worker = this.EntityFacadeManager.GenericEntityFacade.WorkerManager.Copy(source.Worker, copySettings, string.Empty, "WorkItem");
                    }
                }
                if (parent != "WorkItemType")
                {
                    if (copySettings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkItemType", "WorkItemType"))
                    {
                        if (destination.WorkItemType != null)
                            this.EntityFacadeManager.GenericEntityFacade.WorkItemTypeManager.Import(destination.WorkItemType, source.WorkItemType, copySettings, string.Empty, "WorkItem");
                        else if (source.WorkItemType != null)
                            destination.WorkItemType = this.EntityFacadeManager.GenericEntityFacade.WorkItemTypeManager.Copy(source.WorkItemType, copySettings, string.Empty, "WorkItem");
                    }
                }
                if (parent != "WorkRetryPolicy")
                {
                    if (copySettings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkRetryPolicy", "WorkRetryPolicy"))
                    {
                        if (destination.WorkRetryPolicy != null)
                            this.EntityFacadeManager.GenericEntityFacade.WorkRetryPolicyManager.Import(destination.WorkRetryPolicy, source.WorkRetryPolicy, copySettings, string.Empty, "WorkItem");
                        else if (source.WorkRetryPolicy != null)
                            destination.WorkRetryPolicy = this.EntityFacadeManager.GenericEntityFacade.WorkRetryPolicyManager.Copy(source.WorkRetryPolicy, copySettings, string.Empty, "WorkItem");
                    }
                }
            }
            Sidvall.Data.EntityManager.AfterImportItem(destination, source, copySettings);
            OnAfterImport(destination, source, copySettings, parent, child);
        }

        public void Import(System.Collections.Generic.IEnumerable<IWorkItem> destination, System.Collections.Generic.IEnumerable<IWorkItem> source)
        {
            Import(destination, source, null, string.Empty, string.Empty);
        }
        public void Import(System.Collections.Generic.IEnumerable<IWorkItem> destination, System.Collections.Generic.IEnumerable<IWorkItem> source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source, copySettings, string.Empty, string.Empty);
        }
        public void Import(System.Collections.Generic.IEnumerable<IWorkItem> destination, System.Collections.Generic.IEnumerable<IWorkItem> source, long? totalRowCount, Sidvall.Data.CopySettings copySettings)
        {
            Sidvall.Data.ITotalRowCount rowCountProvider;

            if (source == null)
                return;
            Import(destination, source, copySettings, string.Empty, string.Empty);
            if (totalRowCount != null)
            {
                rowCountProvider = destination as Sidvall.Data.ITotalRowCount;
                if (rowCountProvider != null)
                    rowCountProvider.TotalRowCount = totalRowCount.Value;
            }
        }
        internal void Import(Sidvall.Data.IDataRowProvider<IWorkItem> destination, System.Collections.Generic.IEnumerable<IWorkItem> source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            Import(destination.DataItems, source, copySettings, parent, child);
        }
        partial void OnBeforeImport(System.Collections.Generic.IEnumerable<IWorkItem> destination, System.Collections.Generic.IEnumerable<IWorkItem> source, Sidvall.Data.CopySettings copySettings, string parent, string child);
        partial void OnAfterImport(System.Collections.Generic.IEnumerable<IWorkItem> destination, System.Collections.Generic.IEnumerable<IWorkItem> source, Sidvall.Data.CopySettings copySettings, string parent, string child);
        internal void Import(System.Collections.Generic.IEnumerable<IWorkItem> destination, System.Collections.Generic.IEnumerable<IWorkItem> source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            OnBeforeImport(destination, source, copySettings, parent, child);
            ImportItems(destination, source, copySettings, parent, child);
            OnAfterImport(destination, source, copySettings, parent, child);
        }

        #endregion
        #region DataRowIsValid

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        public bool DataRowIsValid(IWorkItem value, Sidvall.Data.BusinessRuleFilter filter)
        {
            if (value == null)
                return true;
            if (filter == null)
                filter = Sidvall.Data.BusinessRuleFilter.CreateFilter();
            var provider = value as Sidvall.Data.IValidParentProvider;
            if ((provider != null) && (!provider.IsSelfValid(filter)))
                return false;
            if (!this.EntityFacadeManager.GenericEntityFacade.WorkItemLogManager.DataRowIsValid(value.WorkItemLogs, filter))
                return false;
            if (!this.EntityFacadeManager.GenericEntityFacade.WorkItemSensorManager.DataRowIsValid(value.WorkItemSensors, filter))
                return false;
            if (!this.EntityFacadeManager.GenericEntityFacade.WorkItemWorkConditionManager.DataRowIsValid(value.WorkItemWorkConditions, filter))
                return false;
            if (!this.EntityFacadeManager.GenericEntityFacade.WorkerManager.DataRowIsValid(value.Worker, filter))
                return false;
            if (!this.EntityFacadeManager.GenericEntityFacade.WorkItemTypeManager.DataRowIsValid(value.WorkItemType, filter))
                return false;
            if (!this.EntityFacadeManager.GenericEntityFacade.WorkRetryPolicyManager.DataRowIsValid(value.WorkRetryPolicy, filter))
                return false;
            return true;
        }
        public bool DataRowIsValid(System.Collections.Generic.IEnumerable<IWorkItem> value, Sidvall.Data.BusinessRuleFilter filter)
        {
            if (value == null)
                return true;
            foreach (var item in value)
            {
                if (!DataRowIsValid(item, filter))
                    return false;
            }
            return true;
        }

        #endregion
        #region DataRowStateIsDirty

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        public bool DataRowStateIsDirty(IWorkItem value)
        {
            if (value == null)
                return false;
            if (value.DataRowStateIsSelfDirty)
                return true;
            if (this.EntityFacadeManager.GenericEntityFacade.WorkItemLogManager.DataRowStateIsDirty(value.WorkItemLogs))
                return true;
            if (this.EntityFacadeManager.GenericEntityFacade.WorkItemSensorManager.DataRowStateIsDirty(value.WorkItemSensors))
                return true;
            if (this.EntityFacadeManager.GenericEntityFacade.WorkItemWorkConditionManager.DataRowStateIsDirty(value.WorkItemWorkConditions))
                return true;
            if (this.EntityFacadeManager.GenericEntityFacade.WorkerManager.DataRowStateIsDirty(value.Worker))
                return true;
            if (this.EntityFacadeManager.GenericEntityFacade.WorkItemTypeManager.DataRowStateIsDirty(value.WorkItemType))
                return true;
            if (this.EntityFacadeManager.GenericEntityFacade.WorkRetryPolicyManager.DataRowStateIsDirty(value.WorkRetryPolicy))
                return true;
            return false;
        }
        public bool DataRowStateIsDirty(System.Collections.Generic.IEnumerable<IWorkItem> value)
        {
            return IsDirtyItems(value);
        }

        #endregion
        #region ValidateAsync

        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(IWorkItem value)
        {
            return await ValidateAsync(value, (Sidvall.Data.ValidationSettings)null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(IWorkItem value, Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await ValidateAsync(value, context).ConfigureAwait(false);
            return context.AllBusinessRules;
        }
        partial void OnBeforeValidate(IWorkItem value, Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        partial void OnAfterValidate(IWorkItem value, Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
        public async System.Threading.Tasks.Task ValidateAsync(IWorkItem value, Sidvall.Business.ValidationContext context)
        {
            if (value == null)
                return;
            var itemBusinessRules = new Sidvall.Data.BusinessRuleCollection();
            OnBeforeValidate(value, itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync().ConfigureAwait(false);
            if (context.Settings.IncludeChildren)
            {
                await this.EntityFacadeManager.GenericEntityFacade.WorkItemLogManager.ValidateAsync(value.WorkItemLogs, context).ConfigureAwait(false);
                await this.EntityFacadeManager.GenericEntityFacade.WorkItemSensorManager.ValidateAsync(value.WorkItemSensors, context).ConfigureAwait(false);
                await this.EntityFacadeManager.GenericEntityFacade.WorkItemWorkConditionManager.ValidateAsync(value.WorkItemWorkConditions, context).ConfigureAwait(false);
            }
            if (context.Settings.IncludeParents)
            {
                await this.EntityFacadeManager.GenericEntityFacade.WorkerManager.ValidateAsync(value.Worker, context).ConfigureAwait(false);
                await this.EntityFacadeManager.GenericEntityFacade.WorkItemTypeManager.ValidateAsync(value.WorkItemType, context).ConfigureAwait(false);
                await this.EntityFacadeManager.GenericEntityFacade.WorkRetryPolicyManager.ValidateAsync(value.WorkRetryPolicy, context).ConfigureAwait(false);
            }
            OnAfterValidate(value, itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync().ConfigureAwait(false);
            context.AllBusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
            var businessRulesProvider = value as Sidvall.Data.IBusinessRulesProvider;
            if (businessRulesProvider != null)
                businessRulesProvider.BusinessRules.Merge(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(System.Collections.Generic.IEnumerable<IWorkItem> value)
        {
            return await ValidateAsync(value, (Sidvall.Data.ValidationSettings)null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(System.Collections.Generic.IEnumerable<IWorkItem> value, Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await ValidateAsync(value, context).ConfigureAwait(false);
            return context.AllBusinessRules;
        }
        public async System.Threading.Tasks.Task ValidateAsync(System.Collections.Generic.IEnumerable<IWorkItem> value, Sidvall.Business.ValidationContext context)
        {
            if (value == null)
                return;
            foreach (var item in value)
                await ValidateAsync(item, context).ConfigureAwait(false);
        }

        #endregion
        #region Initialize

        public void Initialize(IWorkItem value)
        {
            Initialize(value, null);
        }
        partial void OnAfterInitialize(IWorkItem value);
        partial void OnBeforeInitialize(IWorkItem value);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public void Initialize(IWorkItem value, Sidvall.Data.InitializeSettings settings)
        {
            if (value == null)
                return;
            if (settings == null)
                settings = new Sidvall.Data.InitializeSettings();
            OnBeforeInitialize(value);
            var accessLevelProvider = value as Sidvall.Security.IPropertyAccessLevelProvider;
            switch (settings.FieldInitializeOption)
            {
                case Sidvall.Data.FieldInitializeOption.Reset:
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys) == Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, true, true, false, Sidvall.Data.DataType.Int64, "WorkItem.WorkItemId", "WorkItem"))
                            value.WorkItemId = null;
                    }
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys) == Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.CanceledAction", "WorkItem"))
                            value.CanceledAction = global::Sidvall.WorkItemManager.Workflow.ThreadAction.ContinueWithNextWorkItem;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.Description", "WorkItem"))
                            value.Description = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Byte, "WorkItem.ExecutionCompletionPercentage", "WorkItem"))
                            value.ExecutionCompletionPercentage = (byte)0;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.ExecutionContext", "WorkItem"))
                            value.ExecutionContext = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.ExecutionContextUpdated", "WorkItem"))
                            value.ExecutionContextUpdated = null;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.ExecutionOrder", "WorkItem"))
                            value.ExecutionOrder = 0;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.ExecutionStartTime", "WorkItem"))
                            value.ExecutionStartTime = null;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.FailedAction", "WorkItem"))
                            value.FailedAction = global::Sidvall.WorkItemManager.Workflow.ThreadAction.ContinueWithNextWorkItem;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.FailedExecutionCount", "WorkItem"))
                            value.FailedExecutionCount = 0;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "WorkItem.ForceExecution", "WorkItem"))
                            value.ForceExecution = false;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "WorkItem.IsCanceled", "WorkItem"))
                            value.IsCanceled = false;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "WorkItem.IsEnabled", "WorkItem"))
                            value.IsEnabled = false;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.LastExecutionEndTime", "WorkItem"))
                            value.LastExecutionEndTime = null;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.LastExecutionStartTime", "WorkItem"))
                            value.LastExecutionStartTime = null;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.LastResultMessage", "WorkItem"))
                            value.LastResultMessage = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.LastWorkItemResultId", "WorkItem"))
                            value.LastWorkItemResultId = global::Sidvall.WorkItemManager.Workflow.WorkItemResultType.None;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.LogDayCount", "WorkItem"))
                            value.LogDayCount = 0;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.LogModeId", "WorkItem"))
                            value.LogModeId = global::Sidvall.WorkItemManager.Workflow.LogModes.LogAll;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.Name", "WorkItem"))
                            value.Name = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.RetryCount", "WorkItem"))
                            value.RetryCount = 0;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.RetryTime", "WorkItem"))
                            value.RetryTime = null;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.SensorLogDayCount", "WorkItem"))
                            value.SensorLogDayCount = 0;
                        if (settings.IncludedProperty(accessLevelProvider, false, true, false, Sidvall.Data.DataType.Int64, "WorkItem.WorkerId", "WorkItem"))
                            value.WorkerId = 0L;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.WorkItemContext", "WorkItem"))
                            value.WorkItemContext = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.WorkItemStatusId", "WorkItem"))
                            value.WorkItemStatusId = global::Sidvall.WorkItemManager.Workflow.WorkItemStatus.Idle;
                        if (settings.IncludedProperty(accessLevelProvider, false, true, false, Sidvall.Data.DataType.Int64, "WorkItem.WorkItemTypeId", "WorkItem"))
                            value.WorkItemTypeId = null;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.WorkRetryPolicyContext", "WorkItem"))
                            value.WorkRetryPolicyContext = string.Empty;
                        if (settings.IncludedProperty(accessLevelProvider, false, true, false, Sidvall.Data.DataType.Int64, "WorkItem.WorkRetryPolicyId", "WorkItem"))
                            value.WorkRetryPolicyId = null;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.ServerConnectionId", "WorkItem"))
                            value.ServerConnectionId = null;
                    }
                    break;
                case Sidvall.Data.FieldInitializeOption.Validate:
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys) == Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, true, true, false, Sidvall.Data.DataType.Int64, "WorkItem.WorkItemId", "WorkItem"))
                            value.WorkItemId = Sidvall.Business.ValidationManager.GetValidInt64(value.WorkItemId, null, null);
                    }
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys) == Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.Description", "WorkItem"))
                            value.Description = Sidvall.Business.ValidationManager.GetValidString(value.Description, false, null, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Byte, "WorkItem.ExecutionCompletionPercentage", "WorkItem"))
                            value.ExecutionCompletionPercentage = Sidvall.Business.ValidationManager.GetValidByte(value.ExecutionCompletionPercentage, null, null);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.ExecutionContext", "WorkItem"))
                            value.ExecutionContext = Sidvall.Business.ValidationManager.GetValidString(value.ExecutionContext, false, null, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.ExecutionContextUpdated", "WorkItem"))
                            value.ExecutionContextUpdated = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value.ExecutionContextUpdated, System.DateTimeKind.Utc);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.ExecutionOrder", "WorkItem"))
                            value.ExecutionOrder = Sidvall.Business.ValidationManager.GetValidInt32(value.ExecutionOrder, null, null);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.ExecutionStartTime", "WorkItem"))
                            value.ExecutionStartTime = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value.ExecutionStartTime, System.DateTimeKind.Utc);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.FailedExecutionCount", "WorkItem"))
                            value.FailedExecutionCount = Sidvall.Business.ValidationManager.GetValidInt32(value.FailedExecutionCount, null, null);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.LastExecutionEndTime", "WorkItem"))
                            value.LastExecutionEndTime = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value.LastExecutionEndTime, System.DateTimeKind.Utc);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.LastExecutionStartTime", "WorkItem"))
                            value.LastExecutionStartTime = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value.LastExecutionStartTime, System.DateTimeKind.Utc);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.LastResultMessage", "WorkItem"))
                            value.LastResultMessage = Sidvall.Business.ValidationManager.GetValidString(value.LastResultMessage, false, null, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.LogDayCount", "WorkItem"))
                            value.LogDayCount = Sidvall.Business.ValidationManager.GetValidInt32(value.LogDayCount, null, null);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.Name", "WorkItem"))
                            value.Name = Sidvall.Business.ValidationManager.GetValidString(value.Name, false, 255, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.RetryCount", "WorkItem"))
                            value.RetryCount = Sidvall.Business.ValidationManager.GetValidInt32(value.RetryCount, null, null);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "WorkItem.RetryTime", "WorkItem"))
                            value.RetryTime = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value.RetryTime, System.DateTimeKind.Utc);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.SensorLogDayCount", "WorkItem"))
                            value.SensorLogDayCount = Sidvall.Business.ValidationManager.GetValidInt32(value.SensorLogDayCount, null, null);
                        if (settings.IncludedProperty(accessLevelProvider, false, true, false, Sidvall.Data.DataType.Int64, "WorkItem.WorkerId", "WorkItem"))
                            value.WorkerId = Sidvall.Business.ValidationManager.GetValidInt64(value.WorkerId, null, null);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.WorkItemContext", "WorkItem"))
                            value.WorkItemContext = Sidvall.Business.ValidationManager.GetValidString(value.WorkItemContext, false, null, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, true, false, Sidvall.Data.DataType.Int64, "WorkItem.WorkItemTypeId", "WorkItem"))
                            value.WorkItemTypeId = Sidvall.Business.ValidationManager.GetValidInt64(value.WorkItemTypeId, null, null);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.String, "WorkItem.WorkRetryPolicyContext", "WorkItem"))
                            value.WorkRetryPolicyContext = Sidvall.Business.ValidationManager.GetValidString(value.WorkRetryPolicyContext, false, null, true);
                        if (settings.IncludedProperty(accessLevelProvider, false, true, false, Sidvall.Data.DataType.Int64, "WorkItem.WorkRetryPolicyId", "WorkItem"))
                            value.WorkRetryPolicyId = Sidvall.Business.ValidationManager.GetValidInt64(value.WorkRetryPolicyId, null, null);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "WorkItem.ServerConnectionId", "WorkItem"))
                            value.ServerConnectionId = Sidvall.Business.ValidationManager.GetValidInt32(value.ServerConnectionId, null, null);
                    }
                    break;
                default:
                    break;
            }
            if (settings.ClearBusinessRules)
            {
                var businessRulesProvider = value as Sidvall.Data.IBusinessRulesProvider;
                if (businessRulesProvider != null)
                    businessRulesProvider.BusinessRules.Clear();
            }
            switch (settings.ChildInitializeOption)
            {
                case Sidvall.Data.RelationFieldInitializeOption.Include:
                    if (settings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkItemLogs", "WorkItemLog"))
                        this.EntityFacadeManager.GenericEntityFacade.WorkItemLogManager.Initialize(value.WorkItemLogs, settings);
                    if (settings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkItemSensors", "WorkItemSensor"))
                        this.EntityFacadeManager.GenericEntityFacade.WorkItemSensorManager.Initialize(value.WorkItemSensors, settings);
                    if (settings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkItemWorkConditions", "WorkItemWorkCondition"))
                        this.EntityFacadeManager.GenericEntityFacade.WorkItemWorkConditionManager.Initialize(value.WorkItemWorkConditions, settings);
                    break;
                case Sidvall.Data.RelationFieldInitializeOption.Remove:
                    if (settings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkItemLogs", "WorkItemLog"))
                        value.SetWorkItemLogs(null);
                    if (settings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkItemSensors", "WorkItemSensor"))
                        value.SetWorkItemSensors(null);
                    if (settings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkItemWorkConditions", "WorkItemWorkCondition"))
                        value.SetWorkItemWorkConditions(null);
                    break;
                default:
                    break;
            }
            switch (settings.ParentInitializeOption)
            {
                case Sidvall.Data.RelationFieldInitializeOption.Include:
                    if (settings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.Worker", "Worker"))
                        this.EntityFacadeManager.GenericEntityFacade.WorkerManager.Initialize(value.Worker, settings);
                    if (settings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkItemType", "WorkItemType"))
                        this.EntityFacadeManager.GenericEntityFacade.WorkItemTypeManager.Initialize(value.WorkItemType, settings);
                    if (settings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkRetryPolicy", "WorkRetryPolicy"))
                        this.EntityFacadeManager.GenericEntityFacade.WorkRetryPolicyManager.Initialize(value.WorkRetryPolicy, settings);
                    break;
                case Sidvall.Data.RelationFieldInitializeOption.Remove:
                    if (settings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.Worker", "Worker"))
                        value.Worker = null;
                    if (settings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkItemType", "WorkItemType"))
                        value.WorkItemType = null;
                    if (settings.IncludedProperty(accessLevelProvider, false, false, true, Sidvall.Data.DataType.Object, "WorkItem.WorkRetryPolicy", "WorkRetryPolicy"))
                        value.WorkRetryPolicy = null;
                    break;
                default:
                    break;
            }
            OnAfterInitialize(value);
        }
        public void Initialize(System.Collections.Generic.IEnumerable<IWorkItem> value)
        {
            Initialize(value, null);
        }
        public void Initialize(System.Collections.Generic.IEnumerable<IWorkItem> value, Sidvall.Data.InitializeSettings settings)
        {
            InitializeItems(value, settings);
        }

        #endregion
        #region EntityManagerBase Members

        protected override Sidvall.Data.IDataRowContextProvider CopyItem(Sidvall.Data.IDataRowContextProvider destination, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            return Copy(destination as IWorkItem, copySettings, parent, child);
        }
        protected override void ImportItem(Sidvall.Data.IDataRowContextProvider destination, Sidvall.Data.IDataRowContextProvider source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            Import((TEntity)destination, source as IWorkItem, copySettings, parent, child);
        }
        protected override void AcceptItemChanges(Sidvall.Data.IDataRowContextProvider item, Sidvall.Data.AcceptChangesSettings acceptChangesSettings)
        {
            AcceptChanges(item as IWorkItem, acceptChangesSettings);
        }
        protected override bool IsDirtyItem(Sidvall.Data.IDataRowContextProvider item)
        {
            return DataRowStateIsDirty(item as IWorkItem);
        }
        protected override void InitializeItem(Sidvall.Data.IDataRowContextProvider item, Sidvall.Data.InitializeSettings settings)
        {
            Initialize(item as IWorkItem, settings);
        }

        #endregion

        #endregion
        #region Private Members

        private global::Sidvall.WorkItemManager.Data.Model.IEntityFacadeManager EntityFacadeManager { get; set; }

        #endregion
        #region Constructors

        public WorkItemManager(global::Sidvall.WorkItemManager.Data.Model.IEntityFacadeManager entityFacadeManager)
        {
            this.EntityFacadeManager = entityFacadeManager;
        }

        #endregion
    }
}